package cn.skyquant.quant4j.api.order;

import cn.skyquant.quant4j.api.dto.ResultDTO;
import cn.skyquant.quant4j.api.enums.BorkerType;
import cn.skyquant.quant4j.api.service.BaseServiceProxy;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 使用HTTP的代理
 */
public class OrderServiceProxy extends BaseServiceProxy implements OrderService {
    private static final String url = "/order";

    public OrderServiceProxy(String accountCode, BorkerType borkerType) {
        super(accountCode, borkerType);
    }


    @Override
    public ResultDTO<Long> getMaxDate(String accountCode, BorkerType borkerType, String symbol) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("accountCode", accountCode);
        paramMap.put("borkerType", borkerType.name());
        paramMap.put("symbol", symbol);
        JSONObject jsonObject = get(url + "/getMaxDate", paramMap);
        if (jsonObject != null) {
            boolean ok = jsonObject.getBoolean("ok");
            String message = jsonObject.getString("message");
            if (ok) {
                return ResultDTO.success(jsonObject.getLong("data"));
            } else {
                return ResultDTO.error(message);
            }
        }
        return error;
    }

    @Override
    public ResultDTO<Void> insert(AccountOrderHisDTO accountOrderHisDTO) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("id", accountOrderHisDTO.id);
        paramMap.put("borkerType", accountOrderHisDTO.borkerType.name());
        paramMap.put("label", accountOrderHisDTO.label);
        paramMap.put("comment", accountOrderHisDTO.comment);
        paramMap.put("symbol", accountOrderHisDTO.symbol);
        paramMap.put("accountId", accountOrderHisDTO.accountId);
        paramMap.put("command", accountOrderHisDTO.command);
        paramMap.put("amount", String.valueOf(accountOrderHisDTO.amount));
        paramMap.put("openPrice", String.valueOf(accountOrderHisDTO.openPrice));
        paramMap.put("closePrice", String.valueOf(accountOrderHisDTO.closePrice));
        paramMap.put("commission", String.valueOf(accountOrderHisDTO.commission));
        paramMap.put("sl", String.valueOf(accountOrderHisDTO.sl));
        paramMap.put("tp", String.valueOf(accountOrderHisDTO.tp));
        paramMap.put("profit", String.valueOf(accountOrderHisDTO.profit));
        paramMap.put("fillTime", String.valueOf(accountOrderHisDTO.fillTime));
        paramMap.put("creationTime", String.valueOf(accountOrderHisDTO.creationTime));
        paramMap.put("closeTime", String.valueOf(accountOrderHisDTO.closeTime));
        JSONObject jsonObject = post(url, paramMap);
        if (jsonObject != null) {
            boolean ok = jsonObject.getBoolean("ok");
            String message = jsonObject.getString("message");
            if (ok) {
                return ResultDTO.success;
            } else {
                return ResultDTO.error(message);
            }
        }
        return error;
    }
}